package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.Dish;
import com.yyq.gxdc.service.IDishService;
import com.yyq.gxdc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@Controller
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    /**
     * 分页查找
     * @param name
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/front/dishList")
    @ResponseBody
    public Result<IPage<Dish>> routeList(
            String name,
            String categoryName,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize,
            Model model) {

        Result<IPage<Dish>> result = new Result<IPage<Dish>>();
        Page<Dish> page = new Page<Dish>(pageNum, pageSize);
        Dish dish = new Dish();
        dish.setName(name);
        dish.setCategoryName(categoryName);
        IPage<Dish> allRoute = dishService.getAllDish(page, dish);
        result.setSuccess(true);
        result.setResult(allRoute);
        return result;
    }

    /**
     * 后台分页查找
     * @param name
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/mgr/dishList")
    @ResponseBody
    public Result<IPage<Dish>> mgrDishList(
            String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<Dish>> result = new Result<IPage<Dish>>();
        Page<Dish> page = new Page<Dish>(pageNum, pageSize);
        Dish dish = new Dish();
        dish.setName(name);
        IPage<Dish> allRoute = dishService.getAllDish(page, dish);
        result.setSuccess(true);
        result.setResult(allRoute);
        return result;
    }

    /**
     * 详情
     * @param rid
     * @return
     */
    @GetMapping("/getDishDetail")
    @ResponseBody
    public Result<Dish> getRouteDetail(int rid, HttpServletRequest req) {
        Result<Dish> result = new Result<>();
        Dish detailByRid = dishService.getDetailByRid(rid);
        result.setResult(detailByRid);
        result.setSuccess(true);
        return result;
    }

//    /**
//     * 根据rid 获取景点详情图片
//     * @param rid
//     * @return
//     */
//    @GetMapping("/getRouteImgsDetail")
//    @ResponseBody
//    public Result<List<RouteImg>> getRouteImgsDetail(int rid, HttpServletRequest req) {
//        Result<List<RouteImg>> result = new Result<>();
//        List<RouteImg> routeImgByRid = routeImgService.getRouteImgByRid(rid);
//        result.setResult(routeImgByRid);
//        result.setSuccess(true);
//        return result;
//    }

    /**
     * 添加菜品管理信息
     * @param dish
     * @param req
     * @return
     */
    @PostMapping(value = "/addDish")
    @ResponseBody
    public Result<Object> addUser(Dish dish, HttpServletRequest req) {
        // 新增 菜品信息
        dishService.add(dish);
        return Result.ok();
    }

    /**
     * 删除菜品
     * @param id
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteDish")
    @ResponseBody
    public Result<Object> deleteDish(int id, HttpServletRequest req) {
        // 先查询 这个 菜品 是否已存在
        Dish byId = dishService.findById(id);
        if(byId == null ){
            return Result.error("该菜品不存在！");
        }else{
            dishService.delete(id);
            return Result.ok();
        }
    }

    /**
     * 修改菜品
     * @param dish
     * @param req
     * @return
     */
    @PostMapping(value = "/updateDish")
    @ResponseBody
    public Result<Object> updateDish(Dish dish, HttpServletRequest req) {
        // 先查询 这个 行程 是否已存在
        Dish byId = dishService.findById(dish.getId());
        if(byId == null ){
            return Result.error("该菜品不存在！");
        }else{
            dishService.update(dish);
        }
            return Result.ok();
    }

    /**
     * 编辑详情
     * @param id
     * @return
     */
    @GetMapping("/getDetailByRid")
    @ResponseBody
    public Result<Dish> getDetailByRid(Integer id, HttpServletRequest req) {
        Result<Dish> result = new Result<>();
        // 先查询 这个 菜品 是否已存在
        Dish byId = dishService.findById(id);
        if(byId == null ){
            result.setSuccess(false);
            result.setMessage("该菜品不存在！");
            return result;
        }else{
            result.setResult(byId);
            result.setSuccess(true);
            return result;
        }
    }
}
